﻿// **********************************************************************
//
// Author: KingSley
// Email:3268374537@qq.com
// Created:	2018/8/1   10:50
// Introduce: 从Resources文件夹加载预置体，并对加载的物体进行控制
//
// **********************************************************************
using UnityEngine;

namespace KingSley
{
    public class ResourcesObjectController : MonoSingleton<ResourcesObjectController>
    {
        /// <summary>
        /// 将所有资源作为Object加载
        /// </summary>
        /// <typeparam name="T">加载目标类型</typeparam>
        /// <param name="path">加载目标名</param>
        /// <returns>被加载资源Object属性</returns>
        public T LoadResources<T>(string path) where T : Object
        {
            Object obj = Resources.Load(path, typeof(GameObject));
            if (obj == null)
            {
                return null;
            }
            return (T)obj;
        }

        /// <summary>
        /// 实例化预置体目标
        /// </summary>
        /// <param name="obj">加载好的预置体</param>
        public void InstantiateObject(GameObject obj)
        {
            Instantiate(obj);
        }

        /// <summary>
        /// 实例化预置体目标
        /// </summary>
        /// <param name="obj">加载好的预置体</param>
        /// <param name="parents">预置体的父节点</param>
        public void InstantiateObject(GameObject obj, GameObject parents)
        {
            Instantiate(obj);
            obj.transform.parent = parents.transform;
        }

        /// <summary>
        /// 销毁物体
        /// </summary>
        /// <param name="target">销毁目标物体</param>
        public static void DoDestory(Object target)
        {
            if (Application.isPlaying)
                Object.Destroy(target);
            else
                Object.DestroyImmediate(target);
        }
    }
}